package com.lyl.dangdang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lyl.dangdang.pojo.Result;
import com.lyl.dangdang.pojo.po.Address;
import com.lyl.dangdang.mapper.AddressMapper;
import com.lyl.dangdang.service.IAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyl.dangdang.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>

 * @author liyunlong
 * @since 2024-06-09
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements IAddressService {

    @Autowired
    private AddressMapper addressMapper;

    // @Override
    // public Result addAddress(Address address) {
    //     Integer currentUser = ThreadLocalUtil.getCurrentUser();
    //     // LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
    //     // wrapper.eq(Address::getUserId, currentUser);
    //     address.setUserId(currentUser);
    //     // addressMapper.
    //     //       asAddressService.save(address);
    //     addressMapper.insert(address);
    //     return Result.success();
    //
    // }

    @Override
    public Result deleteAddress(Integer addressId) {
        Integer currentUser = ThreadLocalUtil.getCurrentUser();
        addressMapper.deleteByUserId(addressId, currentUser);
        return Result.success();
    }

    @Override
    public Result getUserAddressByAddressId(Integer addressId) {
        Integer currentUser = ThreadLocalUtil.getCurrentUser();
     Address address =  addressMapper.selectByUserIdAndAddressId(addressId, currentUser);
        return Result.success(address);
    }
}
